package models

import (
	"gitee.com/dashengbuqi/luckdraw/persistent/mysql"
)

type LuckdrawAward struct {
	Id        int    `json:"id"`
	Full_name string `json:"full_name"`
	Prize     string `json:"prize"`
	Total     int    `json:"total"`
	Sort      int    `json:"sort"`
	Winner    int    `json:"winner" xorm:"-"`
	Valid     bool   `json:"valid" xorm:"-"`
}

var AwardItems []*LuckdrawAward

func init() {
	var la []*LuckdrawAward
	err := mysql.MysqlEngine.OrderBy("Sort ASC").Find(&la)
	if err != nil {
		panic(err)
	}
	if len(la) > 0 {
		for _, item := range la {
			item.Valid = true
			AwardItems = append(AwardItems, item)
		}
	}
}

//获取排序最小且有效的奖品
func FindDrawLevel() *LuckdrawAward {
	if len(AwardItems) == 0 {
		return nil
	}
	var curDraw *LuckdrawAward
	for _, item := range AwardItems {
		if item.Valid == false {
			continue
		}
		if curDraw == nil {
			curDraw = item
		} else {
			if item.Sort < curDraw.Sort && item.Valid == true {
				curDraw = item
			}
		}
	}
	return curDraw
}
